package com.fish.gaiety.gateway.base;

import com.fish.gaiety.gateway.util.BaseResponseCode;
import com.fish.gaiety.gateway.util.ResponseStatus;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Data
public class ResponseApi<T> {

    private final ResponseStatus status;

    private final String code;

    private final String message;

    private final T response;

    private final String timeStamp;

    public ResponseApi(ResponseStatus status, String code, String message, T response, String timeStamp) {
        this.status = status;
        this.code = code;
        this.message = message;
        this.response = response;
        this.timeStamp = timeStamp;
    }

    public boolean isSucceed(){
        return ResponseStatus.Succeed.equals(status);
    }

    public static BodyBuilder status(BaseResponseCode baseResponseCode){
        return new ResponseApiBuilder(baseResponseCode);
    }

    public interface BodyBuilder{
        <T> ResponseApi<T> body(T body);
    }

    private static class ResponseApiBuilder implements BodyBuilder{

        private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        private final BaseResponseCode baseResponseCode;

        public ResponseApiBuilder(BaseResponseCode baseResponseCode) {
            this.baseResponseCode = baseResponseCode;
        }

        public <T> ResponseApi<T> body(T body){
            ResponseStatus s = baseResponseCode.equals(BaseResponseCode.BASE_SUCCEED)? ResponseStatus.Succeed: ResponseStatus.Error;
            return new ResponseApi<>(s, baseResponseCode.getCode(), baseResponseCode.getMsg(), body, LocalDateTime.now().format(formatter));
        }
    }

}
